﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using DotNetNuke.Entities.Modules;

using DesertedRoadStudios.BlogNetNuke.Common;
using DesertedRoadStudios.BlogNetNuke.Components;
using c = DesertedRoadStudios.BlogNetNuke.Entities.Category;
using t = DesertedRoadStudios.BlogNetNuke.Entities.Tag;
using DesertedRoadStudios.BlogNetNuke.Utilities;

namespace DesertedRoadStudios.BlogNetNuke.UI
{
    public class Post : PortalModuleBase
    {
        private PostInfo _post;
        private XDocument _settings;
        private List<c.CategoryInfo> _categories;
        private List<t.TagInfo> _tags;

        // Post Details
        public Guid PostID { get { return _post.PostID; } }
        public string Title { get { return _post.PostTitle; } }
        public string Slug { get { return _post.Slug; } }
        public string Content { get { return _post.Content; } }
        public string Excerpt { get { return _post.Excerpt; } }
        public bool AllowComments { get { return _post.AllowComments; } }
        public PostStatus Status { get { return _post.Status; } }
        public DateTime PublishedDate { get { return _post.PublishedDate; } }
        public string PostPath { get { return _post.PostPath; } }
        public string Keywords { get { return _post.Keywords; } }
        public string Description { get { return _post.Description; } }
        public string RelativeURL { get { return _settings.Element("params").Element("Blog").Element("BlogDetails").Element("TabPath").Value.Replace("//", "/") + Methods.GetPathExtension(_settings, _post.PostPath); } }
        public string AbsoluteURL { get { return (Request.IsSecureConnection ? "https://" : "http://") + Request.Url.Host + _settings.Element("params").Element("Blog").Element("BlogDetails").Element("TabPath").Value.Replace("//", "/") + Methods.GetPathExtension(_settings, _post.PostPath); } }
        public string PermaLink { get { return (Request.IsSecureConnection ? "https://" : "http://") + Request.Url.Host + Methods.GetPathExtension(_settings, _post.TabPath.Replace("//", "/")) + "?postID=" + _post.PostID; } }
        public int TotalComments { get { return _post.TotalComments; } }
        public List<c.CategoryInfo> Categories { get { return _categories; } }
        public List<t.TagInfo> Tags { get { return _tags; } }

        // Tab Details
        public int PortalID { get { return _post.PortalID; } }
        public int TabID { get { return _post.TabID; } }
        public string TabPath { get { return _settings.Element("params").Element("Blog").Element("BlogDetails").Element("TabPath").Value.Replace("//", "/"); } }
        public int ModuleID { get { return _post.ModuleID; } }

        // User Details
        public int AuthorID { get { return _post.AuthorID; } }
        public DotNetNuke.Entities.Users.UserInfo Author { get { return DotNetNuke.Entities.Users.UserController.GetUserById(_post.PortalID, _post.AuthorID); } }


        public Post()
        {
        }

        public Post(int portalID, int moduleID, Guid postID)
        {
            BlogNetNukeController bnn = new BlogNetNukeController();
            _post = bnn.GetPostByPostID(portalID, moduleID, postID);
            SetSettings(bnn);
            Posts.ParseContent(this, _post.Content);
        }

        public Post(int portalID, int moduleID, DateTime publishedDate, string slug)
        {
            BlogNetNukeController bnn = new BlogNetNukeController();
            _post = bnn.GetPostByDateSlug(portalID, moduleID, publishedDate, slug);
            SetSettings(bnn);
            Posts.ParseContent(this, _post.Content);
        }

        public Post(int portalID, int moduleID, string postPath)
        {
            BlogNetNukeController bnn = new BlogNetNukeController();
            _post = bnn.GetPostByPostPath(portalID, moduleID, postPath);
            SetSettings(bnn);
            Posts.ParseContent(this, _post.Content);
        }

        private void SetSettings(BlogNetNukeController bnn)
        {
            _settings = (XDocument)Session["BlogNetNuke"];

            _categories = new List<c.CategoryInfo>();
            foreach (PostsCategories category in bnn.GetAllCategoriesForPosts(PortalID, ModuleID).Where(c => c.PostID == PostID))
                _categories.Add(new c.CategoryInfo()
                {
                    Category = category.Category,
                    CategoryID = category.CategoryID,
                    Description = category.Description,
                    Keywords = category.Keywords,
                    ModuleID = category.ModuleID,
                    ParentID = category.ParentID,
                    PortalID = category.PortalID,
                    Slug = category.Slug,
                    CategoryLink = Methods.GetPathExtension(_settings, _settings.Element("params").Element("Blog").Element("BlogDetails").Element("TabPath").Value.Replace("//", "/") + "/Category/" + category.Slug)
                });

            _tags = new List<t.TagInfo>();
            foreach (PostsTags tag in bnn.GetAllTagsForPosts(PortalID, ModuleID).Where(t => t.PostID == PostID))
                _tags.Add(new t.TagInfo()
                {
                    Tag = tag.Tag,
                    TagID = tag.TagID,
                    ModuleID = tag.ModuleID,
                    PortalID = tag.PortalID,
                    Slug = tag.Slug,
                    TagLink = Methods.GetPathExtension(_settings, _settings.Element("params").Element("Blog").Element("BlogDetails").Element("TabPath").Value.Replace("//", "/") + "/Tag/" + tag.Slug)
                });
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (_settings.Element("params").Element("RequestType").Value.Equals(RequestType.PostView.ToString()))
            {
                Page.Title = Title;
                if (!string.IsNullOrEmpty(Description))
                {
                    ((HtmlMeta)Page.Header.FindControl("MetaDescription")).Visible = true;
                    ((HtmlMeta)Page.Header.FindControl("MetaDescription")).Content = Description;
                }

                if (!string.IsNullOrEmpty(Keywords))
                {
                    ((HtmlMeta)Page.Header.FindControl("MetaKeywords")).Visible = true;
                    ((HtmlMeta)Page.Header.FindControl("MetaKeywords")).Content = Keywords;
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }
    }
}
